#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
//串的实现

//定义串中数据类型
#define MAX_SIZE 100
//定义串是类型
typedef struct{
    char ch[MAX_SIZE];
    int len;
}SeqString;

//给串赋值
void strAssign(SeqString *str, char *chars){
    if(chars == NULL || str == NULL){
        printf("参数错误");
        return;
    }
    int i =0 ;
    while(chars[i] != '\0'){
        str -> ch[i] = chars[i];
        i++;
    }
    str -> ch[i] = '\0';
    str ->len = i;
    printf("%s\n", str -> ch);
}

//两个串连接
bool strConnect(SeqString *s,SeqString *t){
    if(s->len+t->len>MAX_SIZE){
        printf("拼接后超出范围");
        return;
    }
    for(int i=0;i<t->len;i++){
        s->ch[s->len+i] = t->ch[i];
    }
    s->len += t->len;
    s->ch[s->len]='\0';
}

//比较两个串的大小
int strCompete(SeqString *s,SeqString *t){
    if(s->len >t->len){
        return 1;
    }
    if(s->len <t->len){
        return -1;
    }
    if(s->len==t->len){
        for (int i = 0; i < s->len; ++i) {
            if(s->ch[i]>t->ch[i]){
                return 1;
            }
            if(s->ch[i]<t->ch[i]){
                return -1;
            }
        }
        return 0;
    }
}

//插入
void Insert(SeqString *s,int pos,SeqString *t){
    if (s == NULL || t == NULL) {
        printf("参数错误\n");
        return;
    }
    if (pos < 1 || pos > s->len + 1) {
        printf("插入位置不合法\n");
        return;
    }
    if (s->len + t->len > MAX_SIZE - 1) {
        printf("插入后超过最大容量\n");
        return;
    }

    for (int i = s->len - 1; i >= pos - 1; i--) {
        s->ch[i + t->len] = s->ch[i];
    }

    for (int i = 0; i < t->len; i++) {
        s->ch[pos - 1 + i] = t->ch[i];
    }

    s->len += t->len;
    s->ch[s->len] = '\0';
}
// Delete(): 删除操作:

int main(){
    // 设置控制台为UTF-8编码
    #ifdef _WIN32
    system("chcp 65001 > null");
    #endif

    SeqString s,s1,s2,s3,s4;

    //串赋值
    strAssign(&s, "abc");
    strAssign(&s1, "def");
    strAssign(&s2,"ab c");
    strAssign(&s3,"1234");
    strAssign(&s4,"cdf");


    //串拼接
    strConnect(&s,&s1);
    printf("拼接后：%s",s);

    //比较大小
    printf("\n大小比较结果: %d\n", strCompete(&s1,&s3));

    //插入
    Insert(&s3,2,&s4);
    printf("插入结果：%s",s3);

}
